package com.example.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.AnnouncemtMapper;
import com.example.dao.DepartmentMapper;
import com.example.dao.UserMapper;
import com.example.exception.MyException;
import com.example.pojo.Announcement;
import com.example.pojo.Department;
import com.example.pojo.User;
import com.example.util.DateRangeGenerator;
import com.example.util.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDate;
import java.util.*;


@Slf4j
@Api(tags = "公告管理")
@CrossOrigin
@RestController
public class AnnouncemtController {

    @Autowired
    private AnnouncemtMapper announcemtMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    //添加公告
    @PostMapping("/insertAnct")
    private Result insertAnct(@RequestBody Announcement announcement) {
        int num=announcemtMapper.insert(announcement);
        if (num>0){
            return new Result<>("200","发布成功",announcement);
        }
        return new Result<>("500","发布失败",announcement);
    }
    //查询公告
    @PostMapping("/selectAnct")
    private Result selectAnct( Integer num) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("publish_date");
        queryWrapper.last("limit "+num);
        queryWrapper.eq("approval",4);
        List<Announcement> announcements =announcemtMapper.selectList(queryWrapper);
        return new Result("200","获取公告",announcements);
    }
    //条件查询公告
    @PostMapping("/selectAnctBy")
    private Result selectAnctBy(
            @RequestParam(defaultValue = "") String title,
            @RequestParam(defaultValue = "0") Integer state,
            @RequestParam(defaultValue = "0") Integer publishUnit,
            @RequestParam(defaultValue = "0") Integer userPublish,
            @RequestParam(defaultValue = "false") Boolean test){
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("publish_date");
//        queryWrapper.last("limit "+5);
        if(title!=null&&!title.equals("")){
            queryWrapper.like("title", title);
        }
        if (userPublish!=null&&userPublish!=0){
            queryWrapper.eq("publish_unit",userPublish);
        }
        if (state!=null&&state!=0&&state!=10){
            queryWrapper.eq("approval", state);
        }
        if (state==10){
            queryWrapper.notIn("approval", Arrays.asList(1, 2));
        }
        if (publishUnit!=null&&publishUnit!=0){
            queryWrapper.eq("publish_unit", publishUnit);
        }
        if (test)queryWrapper.eq("approval",4);

        List<Announcement> announcements =announcemtMapper.selectList(queryWrapper);
        for (Announcement announcement : announcements){
//            System.out.println("6666:"+announcement.getPublisher());
            QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",announcement.getPublisher());
            User user=userMapper.selectOne(queryWrapper1);
//            System.out.println("6666666666:"+user);
            if (user!=null) {
                announcement.setPublisherName(user.getName());
            }else {
                announcement.setPublisherName("没找到成员");
            }
            QueryWrapper<Department> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("department_id", announcement.getPublishUnit());
            Department department = departmentMapper.selectOne(queryWrapper2);
            announcement.setPublishUnitName(department.getDepartmentName());
            String text;
            switch (announcement.getApproval()) {
                case 0:
                    text = "已废弃";
                    break;
                case 1:
                    text = "未申报";
                    break;
                case 2:
                    text = "待审批";
                    break;
                case 3:
                    text = "已通过";
                    break;
                case 4:
                    text = "已发布";
                    break;
                case 5:
                    text = "已驳回";
                    break;
                default:
                    text = "状态异常！";
                    // 如果没有匹配的 case，执行这里的代码
            }
            announcement.setState(text);

        }
        return new Result("200","获取公告",announcements);
    }
    //申报
    @PostMapping("/updateAnctBy")
    private Object updateAnctBy(@RequestBody Announcement announcement){
        if (announcement==null) return new MyException("400","参数异常");
        announcement.setPublishDate(LocalDate.now());
        announcemtMapper.updateById(announcement);
        return new Result("200","修改成功");
    }
    //审批
    @PostMapping("/updateAnctByState")
    private Object updateAnctByState(Integer id,Integer num,@RequestParam(defaultValue = "") String value){
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Announcement announcement = announcemtMapper.selectOne(queryWrapper);
        announcement.setApproval(num);
        if (!value.equals("undefined")&&value!=null&&!value.equals(""))announcement.setComments(value);
        announcement.setPublishDate(LocalDate.now());
        announcemtMapper.updateById(announcement);
        return new Result("200","修改成功");
    }
    //查询数量
    @PostMapping("/selectAnnouncementCountByState")
    private Object selectAnnouncementCountByState(Integer state){
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("approval", state);
        queryWrapper.groupBy("approval");
        queryWrapper.select(new String[]{"approval", "COUNT(*) as count"}); // 修正为数组形式
        return new Result("200", "查询", announcemtMapper.selectList(queryWrapper));
    }
    @PostMapping("/selectAnctOne")
    private Object selectAnctOne(Integer id){
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);

        Announcement announcement = announcemtMapper.selectOne(queryWrapper);
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",announcement.getPublisher());
        User user=userMapper.selectOne(queryWrapper1);
        announcement.setPublisherName(user.getName());

        QueryWrapper<Department> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("department_id",announcement.getPublishUnit());
        Department department=departmentMapper.selectOne(queryWrapper2);
        announcement.setPublishUnitName(department.getDepartmentName());
        String text;
        switch (announcement.getApproval()) {
            case 0:
                text="已废弃";
                break;
            case 1:
                text="未申报";
                break;
            case 2:
                text="待审批";
                break;
            case 3:
                text="已通过";
                break;
            case 4:
                text="已发布";
                break;
            case 5:
                text="已驳回";
                break;
            default:
                text="状态异常！";
                // 如果没有匹配的 case，执行这里的代码
        }
        announcement.setState(text);

        return new Result("200","查询成功",announcement);
    }
    @PostMapping("/selectFaBu")
    private Object selectFaBu(){
//        QueryWrapper<Announcement> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.select("publish_date", "COUNT(id) as count");
//        queryWrapper1.groupBy("publish_date");
//        queryWrapper1.orderByAsc("publish_date");
//        queryWrapper1.eq("approval", 4);
//        queryWrapper1.last("limit "+1);
//        LocalDate startDate=announcemtMapper.selectOne(queryWrapper1).getPublishDate();

        LocalDate endDate=LocalDate.now();
        LocalDate startDate=endDate.minusDays(7);
//        List<Announcement> announcements= announcemtMapper.selectList(queryWrapper);


//        LocalDate startDate=LocalDate.now();
//        LocalDate endDate=startDate.plusDays(7);
        List<String> dateRange = DateRangeGenerator.generateDateRange(startDate, endDate);
        List<Announcement> announcements=new ArrayList<>();

        for (String date : dateRange) {
            QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("publish_date", date);
            queryWrapper.eq("approval", 4);
            queryWrapper.select("publish_date", "COUNT(id) as count");
            int count = announcemtMapper.selectOne(queryWrapper).getCount();
            Announcement announcement=new Announcement();
            announcement.setCount(count);
            announcement.setPublishDate(LocalDate.parse(date));
            announcements.add(announcement);
        }


        return new Result("200","查询成功",announcements);
    }
}
